<?php

namespace app\admin\controller;

use think\Request;

//调用model,操作数据库,并取别名
use app\admin\model\Admin as AdminModel;
use app\admin\model\Shoper as ShoperModel;
use app\admin\model\Area as AreaModel;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;

use think\Paginator;


class AuthGroup extends Base
{

    /*************    用户组管理      ***************/
    //角色(用户组)列表
    public function lists()
    {
        //遍历用户表,分页
        $authGroup = AuthGroupModel::where(['group_del'=>0])->paginate(4);
        $groupPage = $authGroup->render();

        //遍历权限规则表
        $authRule = AuthRuleModel::all();
//dump($user);
//dump($userPage);
//dump($data);

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite'     => $webSite,
            'staticAdmin' => $staticAdmin,
            'authGroup'        => $authGroup,
            'groupPage'    => $groupPage,
            'authRule'    => $authRule,

        ]);

        return $this->fetch();


    }

    //显示用户组详情,编辑
    public function edit()
    {
        $uid = Request::instance()->param('uid');

        //角色名
        $roleName = AuthGroupModel::get(['id'=>$uid]);
        $rulesId = $roleName->rules;
        //dump($roleName);

        //获取用户组对应的权限
        $authRule = AuthRuleModel::where(['rule_del'=>0])->where('id','in',"$rulesId")->select();

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite'     => $webSite,
            'staticAdmin' => $staticAdmin,
            'authRule' => $authRule,
            'roleName' => $roleName,
        ]);
        return $this->fetch();
    }

    //ajax查询(获取单条数据)
    public function sel()
    {
        $data['title'] = Request::instance()->param('username');
        //dump($data['title']);

        $data['group_del'] = 0;
        if (Request::instance()->param('rece') == 1) {
            $data['group_del'] = 1;
        }
        //查询
        $result = AuthGroupModel::where($data)->select();

//dump($data);
//dump($result);
        if ($result) {

            //用户
            foreach ($result as $user) {
                //dump($user);
                //防止覆盖
                $data[]['uid'] = $user->id;
                $data[]['username'] = $user->title;
                $data[]['phone'] = $user->rules;

            }

            //获取状态的值和图标
            $active[] =  $user->status == 1 ? '正常' : '禁用';
            $active[] =  $user->id;
            $active[] =  $user->status == 1 ? '&#89;' : 'X';

            //获取用户组对应的权限
            $id = $user->rules;
            $authRule = AuthRuleModel::where(['rule_del'=>0])->where('id','in',"$id")->select();
            // dump($active);
             //dump($data);
            // dump($authRule); //对象(在js中处理了)

            //若存在
            if ($user) {

                //获取值
                $webSite = WEB_SITE;
                $staticAdmin = STATIC_ADMIN;

                echo json_encode(['status' => 1,'webSite' => $webSite, 'staticAdmin' => $staticAdmin, 'res' => $data,'active'=>$active , 'rules'=>$authRule]);

            }
        } else {

            echo json_encode(['status'=>2,'tip'=>'您查询的用户不存在']);
        }

    }

    //修改激活标记位
    public function active()
    {
        $uid = Request::instance()->param('uid');
        $active = Request::instance()->param('active');
        $isActive = $active == 1 ? 0 : 1;   //取反

        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());
        //dump($currTime);

        //修改数据
        $user = AuthGroupModel::where(['id'=>$uid])->update(['status'=>$isActive,'update_time'=>$currTime]);
        //dump($user);
        //dump($isActive);

        if ($user) {
            //更新激活信息
            $active1 =  $isActive == 1 ? '已验证' : '未验证';
            $active2 =  $uid;
            $active3 =  $isActive == 1 ? '&#89;' : 'X';
            $active = [$active1, $active2,$active3];

            //dump($active);
            echo json_encode(['state'=>1,'tip'=>'修改成功','acted'=>$active]);

        } else {
            echo json_encode(['state'=>2,'tip'=>'修改失败']);
        }
    }

    //查看角色名
    public function username()
    {
        $username = Request::instance()->param('username');

        //查询
        $group = AuthGroupModel::get(['title'=>"$username"]);

        if ($group) {

            echo json_encode(['state'=>1,'tip'=>'角色名已存在']);
        } else {

            echo json_encode(['state'=>2,'tip'=>'角色名合法']);
        }
    }

    //角色(用户组)回收
    public function receive()
    {
        //遍历用户表,分页
        $authGroup = AuthGroupModel::where(['group_del'=>1])->paginate(4);
        $groupPage = $authGroup->render();

        //遍历权限规则表
        $authRule = AuthRuleModel::all();

        //传值,渲染
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        $this->assign([
            'webSite'     => $webSite,
            'staticAdmin' => $staticAdmin,
            'authGroup'  => $authGroup,
            'groupPage'   => $groupPage,
            'authRule'   => $authRule,
        ]);

        return $this->fetch();

    }

    //添加页面
    public function increase()
    {
        //遍历权限表
        $authRule = AuthRuleModel::where(['rule_del'=>0])->select();

        //获取值
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        //传值,渲染
        $this->assign([
            'webSite'     => $webSite,
            'staticAdmin' => $staticAdmin,
            'authRule' => $authRule,
        ]);
        return $this->fetch();
    }

    //添加用户组(角色)
    public function add()
    {
        $post  = Request::instance()->param();
        //dump($post);

        $data['title'] = $post['role'];
        $data['rules'] = $post['rules'] . ',102';

        //添加数据
        $group = AuthGroupModel::create($data);

        //是否添加成功
        if ($group) {

            echo json_encode(['state'=>3,'tip'=>'添加角色成功']);
        } else {

            echo json_encode(['state'=>4,'tip'=>'添加角色失败']);
        }
    }

    //编辑修改用户信息
    public function update()
    {
        $uid = Request::instance()->param('uid');
        $data['title'] = Request::instance()->param('str');
        //dump($str);

        //还原关联数组
        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());
        $data['update_time'] = $currTime;
        //dump($data);

        //修改数据
        $group = AuthGroupModel::where(['id'=>$uid])->update($data);
        //dump($user);

        //是否修改成功
        if ($group) {

            echo json_encode(['state'=>5,'tip'=>'修改成功']);
        } else {
            echo json_encode(['state'=>6,'tip'=>'修改失败']);
        }
    }

    //回收用户(伪删除)
    public function discard()
    {
        $uid = Request::instance()->param('uid');
        $flag = Request::instance()->param('flag');
        //dump($uid);
        //dump($flag);

        $groupDel = $flag == 'rever' ? 0 : 1;

        //修改
        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());

        $group = AuthGroupModel::where(['id'=>$uid])->update(['group_del'=>$groupDel,'update_time'=>$currTime]);
        //dump($user);

        //是否修改
        if ($group) {
            //echo json_encode(['state'=>3,'tip'=>'操作成功']);
            if ($flag == 'rever') {
                $this->success('恢复成功');
            } else if ($flag == 'discard') {
                $this->success('回收成功');
            }

        } else {

            //echo json_encode(['state'=>3,'tip'=>'操作失败']);
            if ($flag == 'rever') {
                $this->error('恢复失败');
            } else if ($flag == 'discard') {
                $this->error('回收失败');
            }
        }
    }

    //删除用户
    public function delete()
    {
        $uid = Request::instance()->param('uid');

        //删除数据库
        $group = AuthGroupModel::where(['id'=>$uid])->delete();
        //dump($group);

        //是否删除成功
        if ($group) {

            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }

    }




}